<!DOCTYPE html>
<html lang="en">
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>预约管理
            <small>预约订单管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>预约管理</el-breadcrumb-item>
            <el-breadcrumb-item>预约订单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <!--页面主体-->
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <!--根据日期查询-->
                <template>
                    <el-date-picker
                            v-model="queryDate"
                            type="daterange"
                            range-separator="至"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"
                            unlink-panels style="width: 240px">
                    </el-date-picker>
                    <!--下拉预约类型查询-->
                    <el-select style="width: 125px" v-model="pagination.queryString.orderType" clearable
                               placeholder="请选择">
                        <el-option
                                v-for="t in selectType"
                                :key="t.value"
                                :value="t.value">
                        </el-option>
                    </el-select>
                    <!--下拉预约状态查询-->
                    <el-select style="width: 125px" v-model="pagination.queryString.orderStatus" clearable
                               placeholder="请选择">
                        <el-option
                                v-for="s in selectStatus"
                                :key="s.value"
                                :value="s.value">
                        </el-option>
                    </el-select>
                    <!--下拉套餐名称查迅-->
                    <el-select style="width: 200px" id="setmealSelect" v-model="pagination.queryString.setmealName"
                               clearable
                               placeholder="请选择">
                        <el-option
                                v-for="meal in selectSetmeal"
                                :key="meal.value"
                                :value="meal.value">
                        </el-option>
                    </el-select>
                </template>
                <!--姓名电话查询-->
                <el-input placeholder="会员姓名/手机号" v-model="pagination.queryString.nameOrPhone" style="width: 150px;"
                          class="filter-item" clearable></el-input>
                <el-button @click="pagination.currentPage=1;findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">预约</el-button>
                <!--清除查询条件-->
                <el-button type="primary" class="butT" @click="resetQuery()">清除</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="member.name" label="会员姓名" align="center"></el-table-column>
                <el-table-column prop="member.phoneNumber" label="电话号码" align="center"></el-table-column>
                <el-table-column prop="orderDate" label="预约日期" align="center"></el-table-column>
                <el-table-column prop="orderType" label="预约类型" align="center"></el-table-column>
                <el-table-column prop="id" label="预约id" align="center" v-if="false"></el-table-column>
                <el-table-column label="预约状态" align="center">
                    <template slot-scope="scope">
                        <span>{{scope.row.orderStatus}}</span>
                        <el-switch
                                v-model="scope.row.orderStatus"
                                active-color="#13ce66"
                                inactive-color="#ff4949"
                                active-value="未到诊"
                                inactive-value="已到诊"
                                @change="changeStatus(scope.row)">
                        </el-switch>
                    </template>
                </el-table-column>
                <el-table-column prop="setmeal.name" label="套餐名称" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <!--分页插件-->
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>

            <!-- 电话预约弹层 -->
            <div class="add-form">
                <el-dialog title="新增预约" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="姓名">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="身份证号">
                                                <el-input v-model="formData.idCard"
                                                          @blur="inputIdCard(formData.idCard)"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="出生日期">
                                                <!--日期控件-->
                                                <template>
                                                    <div class="block">
                                                        <el-date-picker
                                                                v-model="formData.birthday"
                                                                type="date"
                                                                value-format="yyyy-MM-dd"
                                                                placeholder="选择日期"
                                                                style="width: 100%">
                                                        </el-date-picker>
                                                    </div>
                                                </template>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="手机号">
                                                <el-input v-model="formData.telephone"
                                                          @blur="inputTelephone(formData.telephone)"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="性别">
                                                <el-select v-model="formData.sex">
                                                    <el-option label="男" value="1"></el-option>
                                                    <el-option label="女" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="年龄">
                                                <el-input v-model="formData.age"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="婚姻状态">
                                                <el-select v-model="formData.marriage">
                                                    <el-option label="未婚" value="0"></el-option>
                                                    <el-option label="已婚" value="1"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="预约时间">
                                                <!--日期控件-->
                                                <template>
                                                    <div class="block">
                                                        <el-date-picker
                                                                v-model="formData.orderDate"
                                                                type="date"
                                                                placeholder="选择日期">
                                                        </el-date-picker>
                                                    </div>
                                                </template>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="套餐信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>套餐编码</th>
                                            <th>套餐名称</th>
                                            <th>套餐说明</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="setmealId" @change="changeSetmealId(c.id)"
                                                       type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.code}}</label></td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.remark}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
            <!-- 修改预约弹窗 -->
            <div class="add-form">
                <el-dialog title="编辑预约" :visible.sync="dialogFormVisibleEdit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col v-if="false">
                                            <el-form-item label="会员id">
                                                <el-input v-model="formData.member.id"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="姓名">
                                                <el-input v-model="formData.member.name"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="身份证号">
                                                <el-input v-model="formData.member.idCard"
                                                          @blur="inputIdCardInEdit(formData.member.idCard)"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="出生日期">
                                                <!--日期控件-->
                                                <template>
                                                    <div class="block">
                                                        <el-date-picker
                                                                v-model="formData.member.birthday"
                                                                type="date"
                                                                value-format="yyyy-MM-dd"
                                                                placeholder="选择日期"
                                                                style="width: 100%">
                                                        </el-date-picker>
                                                    </div>
                                                </template>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="手机号">
                                                <el-input v-model="formData.member.phoneNumber" disabled/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="性别">
                                                <el-select v-model="formData.member.sex">
                                                    <el-option label="男" value="1"></el-option>
                                                    <el-option label="女" value="2"></el-option>
                                                </el-select>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="年龄">
                                                <el-input v-model="formData.member.age"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="婚姻状态">
                                                <el-select v-model="formData.member.marriage">
                                                    <el-option label="未婚" value="0"></el-option>
                                                    <el-option label="已婚" value="1"></el-option>
                                                </el-select>
                                            </el-form-item>
                                            </el-form-item>
                                        </el-col>
                                        <el-col v-if="false">
                                            <el-form-item label="预约id">
                                                <el-input v-model="formData.id"/>
                                            </el-form-item>
                                        </el-col>
                                        <el-col :span="12">
                                            <el-form-item label="预约时间">
                                                <!--日期控件-->
                                                <template>
                                                    <div class="block">
                                                        <el-date-picker
                                                                v-model="formData.orderDate"
                                                                type="date"
                                                                placeholder="选择日期"
                                                                disabled>
                                                        </el-date-picker>
                                                    </div>
                                                </template>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="套餐信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>套餐编码</th>
                                            <th>套餐名称</th>
                                            <th>套餐说明</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="setmealId" @change="changeSetmealId(c.id)" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.code}}</label></td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.remark}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisibleEdit = false">取消</el-button>
                        <el-button type="primary" @click="handlerEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>

        </div>
    </div>
</div>
</body>
<!-- 引入组件库 -->
<script src="../js/vue.js"></script>
<script src="../plugins/elementui/index.js"></script>
<script src="../js/jquery.min.js"></script>
<script src="../js/axios-0.18.0.js"></script>
<script src="../plugins/healthbackend.js"></script>

<script>
    new Vue({
        el: '#app',
        data: {
            pagination: {
                currentPage: 1,//当前页码
                pageSize: 10,//每页显示的记录数
                total: 0,//总记录数
                queryString: {
                    startDate: null,  //开始日期
                    endDate: null,  //结束日期
                    orderType: null,  //预约类型
                    orderStatus: null,  //预约状态
                    setmealName: null,  //套餐名称
                    nameOrPhone: null,  //会员姓名或手机号
                }
            },
            queryDate: null,
            selectType: [{value: '微信预约'}, {value: '电话预约'}],
            selectStatus: [{value: '未到诊'}, {value: '已到诊'}],
            selectSetmeal: [],
            dataList: [],
            status: true,
            dialogFormVisible: false,
            dialogFormVisibleEdit: false, //控制修改窗口显示/隐藏
            activeName: 'first',
            formData: {
                member: {}
            },  //添加预约的表单信息
            tableData: [],  //套餐信息
            setmealId: []
        },
        mounted() {
            this.findPage();
        },
        methods: {
            //分页查询
            findPage() {
                //判断是否输入日期查询
                if (this.queryDate != null) {
                    this.pagination.queryString.startDate = getFormateDate(this.queryDate[0]);
                    this.pagination.queryString.endDate = getFormateDate(this.queryDate[1]);
                }
                axios.get("/orderlist/findPage", {
                    params: {
                        currentPage: this.pagination.currentPage,
                        pageSize: this.pagination.pageSize,
                        queryString: this.pagination.queryString
                    }
                }).then((res) => {
                    if (res.data.flag) {
                        this.dataList = res.data.data.rows;
                        this.resetQuery();
                        this.pagination.total = res.data.data.total; //数据总条数
                        //查询所有的套餐信息
                        axios.get("/setmeal/findAll")
                            .then((res) => {
                                if (res.data.flag) {
                                    this.selectSetmeal = [];
                                    res.data.data.forEach(setmeal => this.selectSetmeal.push({value: setmeal.name}));
                                }
                                else
                                    this.$message.error(res.data.message);
                            })
                    }
                    else
                        this.$message.error(res.data.message);
                });
            },
            //弹窗电话预约弹窗
            handleCreate() {
                //默认展示第一页
                this.activeName = "first";
                //展示弹出框
                this.dialogFormVisible = true;
                //重置表单数据
                this.resetForm();
                //发送异步请求获取套餐数据
                axios.get("/setmeal/findAll")
                    .then(res => {
                        if (res.data.flag) {
                            this.tableData = res.data.data;
                            return;
                        }
                        this.$message.error(res.data.message);
                    })
            },
            //由身份证号自动获取生日及年龄
            inputIdCard(idCard) {
                //校验身份证号码格式是否正确
                if (!checkIdCard(idCard)) {
                    this.$message.error("请输入正确的证件号码");
                } else {
                    //更新生日
                    this.$set(this.formData, 'birthday', getBirthdayFromIdCard(idCard));
                    //更新年龄
                    this.$set(this.formData, 'age', getAgeFromIdCard(idCard));
                }
            },
            inputTelephone(telephone) {
                //校验手机号码是否正确
                if (!checkTelephone(telephone)) {
                    this.$message.error("请输入正确的手机号码");
                }
            },
            //添加预约
            handleAdd() {
                //校验用户姓名是否输入
                if (!this.formData.name) {
                    this.$message.error("请输入姓名");
                }
                else if (!checkTelephone(this.formData.telephone)) {
                    this.$message.error("请输入正确的手机号码");
                }
                else if (!checkIdCard(this.formData.idCard)) {
                    this.$message.error("请输入正确的证件号码");
                }
                //校验套餐选择
                else if (this.setmealId.length == 0) {
                    this.$message.error("请选择体检套餐");
                }
                else if (this.setmealId.length > 1) {
                    this.$message.error("只能选择一个体检套餐");
                } else {
                    //发送异步请求 ,完成数据添加  传会员信息和预约的套餐信息
                    axios.post("/orderlist/add?setmealId=" + this.setmealId[0], this.formData).then(response => {
                        //添加成功
                        if (response.data.flag) {
                            //隐藏弹出框
                            this.dialogFormVisible = false;
                            //展示提示信息
                            this.$message.success(response.data.message);
                            this.findPage();
                            return;
                        }
                        //添加失败
                        this.$message.error(response.data.message);
                    })
                }
            },
            //重置查询条件
            resetQuery() {
                this.pagination.queryString = {};
            },
            // 重置表单
            resetForm() {
                this.formData = {member: {}};
                this.setmealId = [];
            },
            //弹出编辑
            handleUpdate(row) {
                //显示编辑弹出框
                this.dialogFormVisibleEdit = true;
                //重置编辑弹出框数据
                this.resetForm();
                //默认选中第一个选项卡
                this.activeName = "first";
                //查询预约信息
                axios.get("/orderlist/findById?id=" + row.id).then(response => {
                    if (!response.data.flag) {
                        this.$message.error(response.data.message);
                        return;
                    }
                    this.formData = response.data.data;
                    this.setmealId[0] = response.data.data.setmeal.id;
                    this.inputIdCardInEdit(this.formData.member.idCard);
                });
                //发送异步请求获取套餐数据
                axios.get("/setmeal/findAll")
                    .then(res => {
                        if (res.data.flag) {
                            this.tableData = res.data.data;
                            return;
                        }
                        this.$message.error(res.data.message);
                    })
            },
            //由身份证号自动获取生日及年龄
            inputIdCardInEdit(idCard) {
                //校验身份证号码格式是否正确
                if (!checkIdCard(idCard)) {
                    this.$message.error("请输入正确的证件号码");
                } else {
                    //更新生日
                    this.$set(this.formData.member, 'birthday', getBirthdayFromIdCard(idCard));
                    //更新年龄
                    this.$set(this.formData.member, 'age', getAgeFromIdCard(idCard));
                }
            },
            //修改
            handlerEdit() {
                this.formData.setmealId = this.setmealId[0];
                axios.put("/orderlist/updateOrderAndMember", this.formData).then(response => {
                    if (response.data.flag) {
                        this.$message.success(response.data.message);
                        this.dialogFormVisibleEdit = false;
                        this.findPage();
                    } else
                        this.$message.error(response.data.message);
                }).finally(() => {
                    this.resetForm();
                })
            },
            //修改预约状态
            changeStatus(row) {
                //触发修改动作，传入的是修改后的状态
                axios.put("/orderlist/changeStatus", {id: row.id, orderStatus: row.orderStatus})
                    .then((res) => {
                        if (res.data.flag) {
                            this.$message.success(res.data.message);
                            this.dialogFormVisibleEdit = false;
                            this.resetForm();
                        }
                        else {
                            this.$message.error(res.data.message);
                            //修改失败,要变回去
                            row.orderStatus = row.orderStatus.equals('已到诊') ? '未到诊' : '已到诊';
                        }
                    })
            },
            //删除
            handleDelete(row) {
                this.$confirm('确认删除当前选中记录吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    axios.delete("/orderlist/delete?id=" + row.id)
                        .then((res) => {
                            if (res.data.flag) {
                                this.$message.success(res.data.message);
                                this.findPage();
                            }
                            else {
                                this.$message.error(res.data.message);
                            }
                        });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });
            }
            ,
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            changeSetmealId(id) {
                this.setmealId = [];
                this.setmealId[0] = id;
            }
        }
    })
</script>

</html>